<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (! function_exists('collectingHtmlInput'))
{
	/**
	 * $object is array
	 * $except is array
	 */
    function collectingHtmlInput($objects=NULL, $excepts=NULL)
    {
    	$ci=& get_instance();	
    	$fieldNames=array();	
    	if ($objects != NULL){
    		if (is_array($objects)){
    			foreach ($objects as $object){
    				$objectNamespace = "models\entities\\".$object;
					$cmf=$ci->em->getMetadataFactory();
					$class = $cmf->getMetadataFor($objectNamespace);
					
					$tfieldNames = $class->fieldNames;
					$tassocField = $class->associationMappings;
					foreach ($tfieldNames as $field){
						if (!in_array($field, $excepts[$object])){
							$fieldNames[$object][$field] = $ci->input->post($object."_".$field);
						}
					}
					foreach ($tassocField as $field){
						if (!in_array($field['fieldName'], $excepts[$object])){
							$fieldNames[$object][$field['fieldName']] = $ci->input->post($object."_".$field['fieldName']);
						}
					}
							
    			}
			}
		}    	
		
        return $fieldNames;
    }   
	
	/**
	 * @param int $no_of_codes
	 * @param array $exclude_codes_array
	 * @param int $code_length
	 * @return array
	 */
	function generateCode($no_of_codes,$exclude_codes_array='',$code_length = 4)
	{
		$characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
		$promotion_codes = array();
		for($j = 0 ; $j < $no_of_codes; $j++)
		{
			$code = "";
			for ($i = 0; $i < $code_length; $i++)
			{
				$code .= $characters[mt_rand(0, strlen($characters)-1)];
			}
	
			if(!in_array($code,$promotion_codes))
			{
				if(is_array($exclude_codes_array))
				{
					if(!in_array($code,$exclude_codes_array))
					{
						$promotion_codes[$j] = $code;
					}
					else
					{
						$j--;
					}
				}
				else
				{
					$promotion_codes[$j] = $code;
				}
			}
			else
			{
				$j--;
			}
		}
		return $promotion_codes;
	}
	
	function formatrp($angka){
		$rupiah=number_format($angka,2,',','.'); // membentuk tanda pemisah seperti (.)
		return $rupiah;
	}
}